//
//  KZAllOverView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/9/27.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZAllOverView.h"

@implementation KZAllOverView


- (instancetype)init {
    
    if (self = [super init]) {
    
    }
    return self;
}


- (void)setupUI {
    
    UIImageView *iconView = [UIImageView new];
    iconView.image = [UIImage imageNamed:@"kz-全部做完"];
    [self addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.height.equalTo(iconView.mas_width).multipliedBy(428.0/594);
        make.centerY.equalTo(self).offset(-60);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.layer.cornerRadius = 4;
    button.layer.masksToBounds = true;
    button.backgroundColor = KZThemeColor;
    button.titleLabel.font = FONT(15);
    [button setTitle:@"好的" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self).multipliedBy(0.7);
        make.height.equalTo(@40);
        make.centerX.equalTo(@0);
        make.top.equalTo(iconView.mas_bottom).offset(20);
        
        make.bottom.equalTo(@0);
    }];
}

- (void)buttonAction:(UIButton *)button {
    
    [self hide];
    if (_okBlock) {
        _okBlock();
    }
}



- (void)show {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    UIView *maskView = [UIView new];
    maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.0];
    [window addSubview:maskView];
    [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(window);
    }];
    
    [maskView addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(maskView).multipliedBy(0.8);
        make.center.equalTo(maskView);
        // 高度根据子视图
    }];
    
    [self layoutIfNeeded];
    [self setupUI];
    
    self.transform = CGAffineTransformMakeScale(0.01, 0.01);
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.transform = CGAffineTransformIdentity;
        maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.5];
    } completion:^(BOOL finished) {
    }];
}
- (void)hide {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.transform = CGAffineTransformMakeScale(0.01, 0.01);
        self.superview.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.1];
        
    } completion:^(BOOL finished) {
        if (finished) {
            [self.superview removeFromSuperview];
        }
    }];
}

@end
